﻿using FarmSmartSleep.Common.Domain.PM;
using FarmSmartSleep.Common.Dtos;
using FarmSmartSleep.Common.Infrastructure;
using MediatR;

namespace FarmSmartSleep.Organ.API.Application.ProductionManagement.MotherHybridizationCommands
{
    public class MotherHybridizationAllDeleteHander : IRequestHandler<MotherHybridizationAllDeleteCommand, Result>
    {


        private readonly IBaseRepository<MotherHybridization> repository;
        private readonly ILogger<MotherHybridizationAllDeleteHander> logger;

        public MotherHybridizationAllDeleteHander(IBaseRepository<MotherHybridization> repository, ILogger<MotherHybridizationAllDeleteHander> logger)
        {
            this.repository = repository;
            this.logger = logger;
        }


        /// <summary>
        /// 删除配种信息（批量）
        /// </summary>
        /// <param name="request">删除配种信息请求对象</param>
        /// <param name="cancellationToken"></param>
        /// <returns>删除配种信息响应对象</returns>
        public async Task<Result> Handle(MotherHybridizationAllDeleteCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var result = new Result();
                string[] strings = request.Ids.Split(',');
                foreach (var item in strings)
                {
                    var info = repository.GetAll().FirstOrDefault(c => c.MotherHybridizationId == Convert.ToInt32(item));
                    info.Deletes = true;
                    info.DeleteId = request.DeleteId;
                    info.DeleteTime = DateTime.Now;
                    result.Code +=  repository.Update(info);
                }
                logger.LogInformation("批量删除配种信息方法调用成功");
                return result;
            }
            catch (Exception ex )
            {
                logger.LogError("删除配种信息方法调用出错" + ex);
                throw;
            }
        }
    }
}
